/**
 * @author GZY
 */

package sg.edu.nus.iss.team4ft.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * This class is the utility for formatting date. 
 */
public class DateUtil {
    public static final String DATE_FORMAT = "dd-MMM-yyyy";
    public static final String DATE_UI = "MM-yyyy";
    public static final String TXN_DATE_FORMAT = "dd-MMM-yyyy HH:mm:ss";
    public static final String timezone = "Asia/Singapore";
    public static final String YEAR = "yyyy";
    public static final String MONTH = "MMM";
    public static final String DEFAULT_FORMAT = "MMM dd, yyyy";
    public static final Item[] MONTHS_VALUE = {	new Item(1,"Jan"),
    											new Item(2,"Feb"), 
    											new Item(3, "Mar"),
    											new Item(4, "Apr"),
    											new Item(5, "May"),
    											new Item(6, "Jun"),
    											new Item(7, "Jul"),
    											new Item(8, "Aug"),
    											new Item(9, "Sep"),
    											new Item(10, "Oct"),
    											new Item(11, "Nov"),
    											new Item(12, "Dec")
    										};
    
    public static final String[] YEAR_SELECT = {	"2012",
    												"2011" 
    									  		};
    
    /**
     * Get the default format of the given date instance.
     */
    public static String defaultFormat (Date date) {
        if (date == null)
            return null;
        
        return format(date,null);
    }

    /**
     * Get the format of the given date instance based on the date format provided.
     */
    public static String format (Date date, String format) {
        if (date == null)
            return null;
        
        if(format == null)
        	format = DEFAULT_FORMAT; 

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * Returns the date instance based on the current system date
     */
    public static Date getDate () {
        TimeZone tz = TimeZone.getTimeZone(timezone);
        return Calendar.getInstance(tz).getTime();
    }
    
    
    /**
     * Adds the specified amount of time to the given time field.
     */
    public static Date add (Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);

        return cal.getTime();
    }
    
    /**
     * Checks if the date is valid
     */
    public static boolean isValidDate (int year, int month, int day) {

        if (day <= 0 || month <= 0 || year <= 0)
            return false;
        if (month > 12 || day > 31)
            return false;

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);

        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (day > maxDay)
            return false;

        return true;
    }
    
    /**
     * Parse a String to form a Date object based on the format given
     */
    public static Date parse (String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setLenient(false);
        TimeZone tz = TimeZone.getTimeZone(timezone);
        sdf.setTimeZone(tz);
        return sdf.parse(date);
    }
    
    /**
     * Get the maximum date in a month
     * @param date
     * @return
     */
	public static Date getMaxDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, cal .getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
		
		return cal.getTime();
	}
	
	/**
	 * Date comparison
	 * @param firstDate
	 * @param secondDate
	 * @return true if first date is before second date
	 */
	public static boolean dateIsBefore(Date firstDate, Date secondDate){

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(firstDate);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(secondDate);
		
		if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH))
			return false;
		
		if(cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)){
			if(cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR))
				return true;
			else
				return false;
		}
		
		if(cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)){
			if(cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH))
				return true;
			else
				return false;
		}
		
		if(cal1.get(Calendar.DAY_OF_MONTH) != cal2.get(Calendar.DAY_OF_MONTH))
			if(cal1.get(Calendar.DAY_OF_MONTH) < cal2.get(Calendar.DAY_OF_MONTH))
				return true;
			else
				return false;
			
		return false;
	}
	
	public static boolean dateIsEqual(Date firstDate, Date secondDate){

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(firstDate);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(secondDate);
		
		if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH))
			return true;
		
		
		return false;
	}
	
	public static boolean dateMonthAndYearIsEqual(Date firstDate, Date secondDate){

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(firstDate);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(secondDate);
		
		if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH))
			return true;
		
		
		return false;
	}
}